//
//  CertGeneralName.m
//  TestProject
//
//  Created by 武侠 on 2022/2/18.
//  Copyright © 2022 zhulong. All rights reserved.
//

#import "CertGeneralName.h"
#import "CertItemLength.h"
#import "CertTLV.h"

@implementation CertGeneralName

- (NSDictionary<NSNumber *,NSString *> *)objectType {
    return @{
        @(0x82) : @"dns",
        @(0xa4) : @"directory",
        @(0x86) : @"uri"
    };
}

// 根据类型：返回对应的类
- (id _Nullable)getObject:(NSData *)data type:(uint16_t)type {
    char *sdata = (char *)data.bytes;
    
    if (type == 0xa4) {
        CertItemLength *length = [[CertItemLength alloc] initWithData:sdata index:1];
        NSData *vData = [NSData dataWithBytes:sdata+1+length.lenLen length:length.len];
        return [self getList:vData type:type];
    } else if (type == 0x1a4) {
        return [[CertOID alloc] initWithData:data];
    } else if (type == 0x82) {
        return [CertTLV value:sdata begin:0];
    } else if (type == 0x86) {
        return [CertTLV value:sdata begin:0];
    } else {
        return [super getObject:data type:type];
    }
}

- (NSString *)description {
    NSMutableString *str = [NSMutableString string];
    
    if (self.dns.length > 0) {
        [str appendFormat:@"dns = %@\n", self.dns];
    }
    
    NSMutableString *directrorys = [NSMutableString string];
    for (int i = 0; i < self.directory.count; i++) {
        if (i == 0) {
            [directrorys appendFormat:@"%@=%@", self.directory[i].oidDesc, self.directory[i].value];
        } else {
            [directrorys appendFormat:@", %@=%@", self.directory[i].oidDesc, self.directory[i].value];
        }
    }
    if (directrorys.length > 0) {
        [directrorys insertString:@"directoryName = [" atIndex:0];
        [directrorys appendString:@"]\n"];
        [str appendString:directrorys];
    }
    
    if (self.uri.length > 0) {
        [str appendFormat:@"URI = %@\n", self.uri];
    }
    
    if (str.length > 0) {
        [str deleteCharactersInRange:NSMakeRange(str.length-1, 1)];
    }
    return str.length > 0 ? str : @"无数据";
}

@end
